package com.ssy.lingxi.member.merchant.serviceimpl.base;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.config.ServiceConfig;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.base.IBaseSiteService;
import com.ssy.lingxi.platform.manage.api.feign.inner.SiteControllerFeign;
import com.ssy.lingxi.platform.manage.api.model.dto.site.SiteIdDTO;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 站点信息相关接口
 *
 * @author whm
 * @version 2.0.0
 * @date 2022-03-14
 **/
@Service
public class BaseSiteServiceImpl implements IBaseSiteService {
    @Resource
    private IBaseMemberCacheService baseMemberCacheService;

    @Resource
    private SiteControllerFeign siteControllerFeign;

    /**
     * 校验是否开启SAAS多租户部署
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @Override
    public Wrapper<Boolean> isEnableMultiTenancy(HttpHeaders headers) {
        SiteIdDTO siteIdDTO = new SiteIdDTO();
        siteIdDTO.setSiteId(baseMemberCacheService.getLongValueFromRequestHeader(headers, ServiceConfig.USER_LOGIN_SITE_HTTP_HEADERS_KEY));
        Wrapper<Boolean> wrapper = siteControllerFeign.enableMultiTenancy(siteIdDTO);
        if (wrapper == null || wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            throw new BusinessException(ResponseCode.SERVICE_PLATFORM_MANAGE_ERROR);
        }
        return wrapper;
    }
}
